package com.cgtong.android.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cgtong.android.R;
import com.cgtong.android.adapter.ImagePreviewAdapter;
import com.cgtong.android.bean.ProblemBean;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class LocalImagePreviewActivity extends AppCompatActivity {

    public static final String IMAGE_INFO = "IMAGE_INFO";
    public static final String CURRENT_ITEM = "CURRENT_ITEM";
    public static final int ANIMATE_DURATION = 200;
    @BindView(R.id.show_original_txt)
    TextView mShowOriginalTxt;


    public static void start(Context context, List<ProblemBean.ResourceBean> urls, int curIndex) {
        Intent intent = new Intent(context, LocalImagePreviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO, (Serializable) urls);
        bundle.putInt(ImagePreviewActivity.CURRENT_ITEM, curIndex);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    private RelativeLayout rootView;
    private ImagePreviewAdapter adapter;
    private List<ProblemBean.ResourceBean> imageInfo;
    private int currentItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        ButterKnife.bind(this);
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        final TextView pageIndextTxt = (TextView) findViewById(R.id.page_txt);
        rootView = (RelativeLayout) findViewById(R.id.root_view);
        mShowOriginalTxt.setVisibility(View.GONE);
        Intent intent = getIntent();
        imageInfo = (List<ProblemBean.ResourceBean>) intent.getSerializableExtra(IMAGE_INFO);
        currentItem = intent.getIntExtra(CURRENT_ITEM, 0);

        adapter = new ImagePreviewAdapter(this, imageInfo);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(currentItem);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                currentItem = position;
                pageIndextTxt.setText(String.format(getString(R.string.select), String.valueOf(currentItem + 1), String.valueOf(imageInfo.size())));
            }
        });
        pageIndextTxt.setText(String.format(getString(R.string.select), String.valueOf(currentItem + 1), String.valueOf(imageInfo.size())));
    }

}
